package com.whfc.base.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import liquibase.integration.spring.SpringLiquibase;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Description:
 * @Author: likang
 * @Date: 2020-10-29 10:40
 */
@Configuration
@MapperScan(basePackages = "com.whfc.base.dao", sqlSessionFactoryRef = "baseSqlSessionFactory")
public class BaseDataSourceConfig {

    @NacosValue(value = "${spring.datasource.base.url}", autoRefreshed = true)
    private String baseDbUrl;

    @NacosValue(value = "${spring.datasource.base.username}", autoRefreshed = true)
    private String baseDbUsername;

    @NacosValue(value = "${spring.datasource.base.password}", autoRefreshed = true)
    private String baseDbPassword;

    @NacosValue(value = "${spring.datasource.base.driver-class-name}", autoRefreshed = true)
    private String baseDbDirver;

    @Bean(name = "baseDataSource")
    public DataSource baseDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(baseDbUrl);
        datasource.setUsername(baseDbUsername);
        datasource.setPassword(baseDbPassword);
        datasource.setDriverClassName(baseDbDirver);
        datasource.setValidationQuery("select 1");
        datasource.setValidationQueryTimeout(60000);
        return datasource;
    }

    @Bean(name = "baseSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/whfc/base/mapper/*.xml"));
        bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/mybatis-config.xml"));
        return bean.getObject();
    }

    @Bean(name = "baseTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SpringLiquibase liquibase(@Qualifier("baseDataSource") DataSource dataSource) throws SQLException {

        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:liquibase/master.xml");
        liquibase.setShouldRun(true);
        liquibase.setDropFirst(false);
        return liquibase;
    }
}
